package org.carshared.sqlite;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import org.carshared.utils.DateTravelBeen;
import org.carshared.utils.PassengerBeen;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class DBAdapterDateTravel {

	// Campos de la BD
	public static final String COLUMN_ID_VIAJE = "idViaje";
	public static final String COLUMN_NOMBRE = "nombrePasajero";
	public static final String COLUMN_FECHA = "fecha";

	private static final String DATABASE_TABLE_DATETRAVEL = "DatePassengerTravel";
	private SQLiteDatabase database;
	private MySQLiteHelper dbHelper;
	private Context context;

	public DBAdapterDateTravel(Context context) {
		this.context = context;
		dbHelper = new MySQLiteHelper(context);
	}

	public DBAdapterDateTravel open() throws SQLException {
		database = dbHelper.getWritableDatabase();
		return this;
	}

	public void close() {
		dbHelper.close();
	}

	/**
	 * Crea una nueva tarea, si esto va bien retorna la rowId de la tarea, de lo
	 * contrario retorna -1
	 */
	public long createDateTravelBeen(DateTravelBeen d) {
		open();
		ContentValues initialValues = createContentValues(d);
		long i = database
				.insert(DATABASE_TABLE_DATETRAVEL, null, initialValues);
		close();
		return i;
	}

	// Actualiza la tarea
	public void updatePassengerl(DateTravelBeen d) {
		open();
		ContentValues updateValues = createContentValues(d);

		database.update(DATABASE_TABLE_DATETRAVEL, updateValues, null, null);
		close();
	}

	// Borra la tarea
	public int deleteDataPassenger(PassengerBeen p) {
		open();
		int i = database.delete(DATABASE_TABLE_DATETRAVEL, COLUMN_NOMBRE + "="
				+ "'" + p.getNombrePasajero() + "'" + " AND " + COLUMN_ID_VIAJE
				+ "=" + "'" + p.getIdViaje() + "'", null);
		close();
		return i;
	}

	public int deleteAll(String idViaje) {
		open();
		int i = database.delete(DATABASE_TABLE_DATETRAVEL, COLUMN_ID_VIAJE
				+ "=" + "'" + idViaje + "'", null);
		close();
		return i;
	}

	// Returna un Cursor que contiene todos los items
	// public ArrayList<DateTravelBeen> fetchAllDateTravels(String rowId) {
	// open();
	// Cursor mCursor = database.query(DATABASE_TABLE_DATETRAVEL,
	// new String[] { COLUMN_ID_VIAJE, COLUMN_NOMBRE, COLUMN_FECHA },
	// COLUMN_ID_VIAJE + "=" + "'" + rowId + "'", null, null, null,
	// null);
	// return cursorToArrayList(mCursor);
	// }

	// Returna un Cursor que contiene la info del item
	public ArrayList<String> fetchDateTravelBeen(String travelId,
			String namePassenger) throws SQLException {
		open();
		Cursor mCursor = database.query(true, DATABASE_TABLE_DATETRAVEL,
				new String[] { COLUMN_FECHA }, COLUMN_ID_VIAJE + "='"
						+ travelId + "' AND " + COLUMN_NOMBRE + "='"
						+ namePassenger + "'", null, null, null, null, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}

		ArrayList<String> result = new ArrayList<String>();
		if (mCursor.getCount() > 0) {
			result = cursorToArrayList(mCursor);
		}

		// if (mCursor.getCount() > 1) {

		// } else {
		// Calendar cal = Calendar.getInstance();
		// Date date = null;
		// try {
		// date = new SimpleDateFormat("dd-MM-yyyy").parse(mCursor
		// .getString(0));
		// } catch (ParseException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
		//
		// cal.setTime(date);
		// result.add(cal);
		// }

		return result;
	}

	private ArrayList<String> cursorToArrayList(Cursor c) {

		ArrayList<String> result = new ArrayList<String>();

		do {

			result.add(c.getString(0));

		} while (c.moveToNext());

		close();
		return result;
	}

	private String calendarToString(Date date) {
		SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
		String currentDate = formatter.format(date);
		return currentDate;
	}

	private ContentValues createContentValues(DateTravelBeen date) {
		ContentValues values = new ContentValues();

		values.put(COLUMN_FECHA, calendarToString(date.getFecha().getTime()));
		values.put(COLUMN_ID_VIAJE, date.getOrigenDestino());
		values.put(COLUMN_NOMBRE, date.getNombrePasajero());

		return values;
	}

}
